package com.xjscrm.console.web.job;

import com.xjscrm.console.service.common.xxljob.XxlJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class JobController {

    private static Logger logger = LoggerFactory.getLogger(JobController.class);
    @Autowired
    private XxlJobService xxlJobService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 0 * * ?")
    public Object clearAcceptToday() throws Exception {
        logger.info("清理每日已接受邀请数 (每天凌晨执行一次) 开始执行");
        xxlJobService.clearAcceptToday();
        return true;
    }

    @Scheduled(cron = "0/30 * * * * ?")
    public Object excuteTask() throws Exception {
        logger.info("执行群邀请任务 (每30s执行一次) 开始执行");
        xxlJobService.excuteTask();
        return true;
    }

    @Scheduled(cron = "0 */2 * * * *")
    public Object syncCustomer() throws Exception {
        logger.info("执行同步通讯录任务 (每2min执行一次) 开始执行");
        xxlJobService.syncCustomer();
        return true;
    }

    @Scheduled(cron = "0 */2 * * * *")
    public Object syncGroup() throws Exception {
        logger.info("执行同步客户群任务 (每2min执行一次) 开始执行");
        xxlJobService.syncGroup();
        return true;
    }


    @Scheduled(cron = "0 */2 * * * *")
    public Object syncPersonTag() throws Exception {
        logger.info("执行同步个人标签任务 (每2min执行一次) 开始执行");
        xxlJobService.syncPersonTag();
        return true;
    }

    @Scheduled(cron = "*/5 * * * * *")
    public Object autoGroupSendTaskSyncByImmediately() throws Exception {
        logger.info("处理立即或者定时发送的极速群发任务 (每5s执行一次) 开始执行");
        xxlJobService.autoGroupSendTaskSyncByImmediately();
        return true;
    }


    @Scheduled(cron = "0/30 * * * * ?")
    public Object autoGroupSendTaskSyncByCycle() throws Exception {
        logger.info("处理立即或者定时发送的极速群发任务 (每30s执行一次) 开始执行");
        xxlJobService.autoGroupSendTaskSyncByCycle();
        return true;
    }

    @Scheduled(cron = "0/30 * * * * ?")
    public Object autoGroupSendJob() throws Exception {

        InetAddress inetAddress = InetAddress.getLocalHost();
        String serverIp = inetAddress.getHostAddress();
        if (serverIp.equals("192.168.31.201")) {
            return true;
        }
            logger.info("处理极速群发的job (每30s执行一次) 开始执行");
            xxlJobService.autoGroupSendJob();
        return true;
    }

    @Scheduled(cron = "0 10 0 ? * *")
    public Object cleanRecord() throws Exception {
        logger.info("执行删除聊天记录 (每天00:10执行一次) 开始执行");
//        xxlJobService.executeCleanChatTalkRecord();
        return true;
    }
}
